package com.ds.dsm.view.config.menu.service;

import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.view.config.menu.FormMenuPopTree;
import com.ds.dsm.view.config.nav.NavMenuPopTree;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.annotation.PopTreeViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.NavBaseViewBean;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping(path = "/dsm/view/config/nav/")
public class NavMenuService {


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "常用按钮")
    @RequestMapping("NavMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<NavMenuPopTree>> getMenuPopTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<NavMenuPopTree>> model = new TreeListResultModel<>();
        List<NavMenuPopTree> popTrees = new ArrayList<>();
        try {
            NavMenuPopTree menuPopTree = new NavMenuPopTree("NavMenu", "常用按钮", "NavMenuTree");
            for (CustomFormMenu menu : CustomFormMenu.values()) {
                NavMenuPopTree item = new NavMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }

    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "下标按钮")
    @RequestMapping("NavBottomTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<NavMenuPopTree>> getMenuBottomTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<NavMenuPopTree>> model = new TreeListResultModel<>();
        List<NavMenuPopTree> popTrees = new ArrayList<>();
        try {
            NavMenuPopTree menuPopTree = new NavMenuPopTree("NavBottombar", "下标按钮", "NavBottomTree");
            for (CustomFormMenu menu : CustomFormMenu.values()) {
                FormMenuPopTree item = new FormMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @RequestMapping("addMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.treeSave, callback = {CustomCallBack.ReloadParent, CustomCallBack.Close})
    @ResponseBody
    public ResultModel<Boolean> addMenu(String sourceClassName, String methodName, String NavMenuTree, String NavBottomTree,String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            NavBaseViewBean formViewBean = (NavBaseViewBean) methodAPIBean.getView();
            if (NavMenuTree != null && !NavMenuTree.equals("")) {
                String[] menuIds = StringUtility.split(NavMenuTree, ";");
                for (String menuId : menuIds) {
                    if (menuId != null && !menuIds.equals("")) {
                        formViewBean.getCustomMenu().add(CustomFormMenu.valueOf(menuId));
                    }
                }

            } else if (NavBottomTree != null && !NavBottomTree.equals("")) {
                String[] menuIds = StringUtility.split(NavBottomTree, ";");
                for (String menuId : menuIds) {
                    if (menuId != null && !menuIds.equals("")) {
                        formViewBean.getBottombarMenu().add(CustomFormMenu.valueOf(menuId));
                    }
                }
            }
            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }

        return model;
    }


    @RequestMapping("delMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.delete, callback = {CustomCallBack.Reload})
    @ResponseBody
    public ResultModel<Boolean> delMenu(String sourceClassName, String methodName, String type, String bar,String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            NavBaseViewBean formViewBean = (NavBaseViewBean) methodAPIBean.getView();

            String[] menuIds = StringUtility.split(type, ";");
            for (String menuId : menuIds) {
                formViewBean.getCustomMenu().remove(CustomFormMenu.valueOf(menuId));
                formViewBean.getBottombarMenu().remove(CustomFormMenu.valueOf(menuId));
            }
            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }
        return model;
    }


}
